import {Injectable} from '@angular/core';
import {Headers, RequestOptions} from '@angular/http';
import {adSystemApiUrl} from '../../../../../config/api';
import {HttpService} from '../../../../lib/service/http.service';

@Injectable()

export class LotterySpellService {

  constructor(private httpService: HttpService) {
  }

  // 获取分组列表
  getGroupList() {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category/list`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取商品列表
  getGoodsList(page: number,
               pageSize: number,
               applet_goods_category_id?: number,
               status?: string, order?: string,
               keyword?: string) {
    const _applet_goods_category_id = applet_goods_category_id || '',
      _status = (status === '选择商品状态' ? '' : status) || '',
      _keyword = keyword || '';
    const _order = (order === 'none' ? '' : order) || '';
    const url = `${adSystemApiUrl}/v1/member/goods/list` +
      `?applet_goods_category_id=${_applet_goods_category_id}` +
      `&order=${_order}&status=${_status}&keyword=${_keyword}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取卡券列表
  getCouponList(page: number,
                pageSize: number,
                keyword: string) {
    const url = `${adSystemApiUrl}/v2/member/card_coupons/list`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      page: page,
      pageSize: pageSize,
      keyword: keyword
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 获取商品绑定了哪些分类
  getbindListAll(goods_id) {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category_relation/bindListAll?goods_id=${goods_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取商品sku
  getGoodsSkuList(goods_id: number) {
    const url = `${adSystemApiUrl}/v1/member/goods_sku/list?goods_id=${goods_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 添加一分拼团
  addPenny(data) {
    const url = `${adSystemApiUrl}/v2/member/activity/addPenny`;
    return this.httpService.httpPostAuth(url, data).map(rs => rs);
  }

  // 获取一分拼团列表
  getPennyList( page, pageSize, status) {
    let stage;
    if (status === '未开始') {
      stage = 1;
    } else if (status === '进行中') {
      stage = 2;
    } else {
      stage = 3;
    }
    const url = `${adSystemApiUrl}/v2/member/activity/penny_list?page=${page}&pageSize=${pageSize}&stage=${stage}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取一分拼团详情
  getPennyInfo(id) {
    const url = `${adSystemApiUrl}/v2/member/activity/pennyInfo?id=${id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 修改一分拼团状态
  changePennyStatus(id, penny_status) {
    const url = `${adSystemApiUrl}/v2/member/activity/penny_status`;
    const status = (penny_status === '正常' ? 0 : 1);
    const data = {
      id: id,
      status: status
    };
    return this.httpService.httpPostAuth(url, data).map(rs => rs);
  }

  // 修改一分拼团
  editPenny(data) {
    const url = `${adSystemApiUrl}/v2/member/activity/editPenny`;
    return this.httpService.httpPostAuth(url, data).map(rs => rs);
  }

  // 获取中奖详情
  getPrizeWinner(id) {
    const url = `${adSystemApiUrl}/v2/member/activity_goods_prize_winner/get_info_by_activity_id?activity_id=${id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 删除活动
  delActivity(id) {
    const data = {id: id};
    const url = `${adSystemApiUrl}/v2/member/activity/del`;
    return this.httpService.httpPostAuth(url, data).map(rs => rs);
  }

  // 判断是否有模板
  judge(applet_uuid) {
    const url = `${adSystemApiUrl}/v1/member/wechat/judge_whether_shop_has_template?applet_uuid=${applet_uuid}&wx_id=AT0705`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }
}
